package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
@Slf4j

@RestController("backendSetmealController")
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    //新增套餐------------------
    @PostMapping("/add")
    Result addSetmeal(@RequestBody Setmeal setmeal, @RequestParam ArrayList<Long>checkgroupIds){
        log.info("新增套餐：{},{}", setmeal,checkgroupIds);
        setmealService.addSetmeal(setmeal,checkgroupIds);
        return  new Result(true,"新增套餐成功");
    }
    //编辑套餐----------------------
    @PostMapping("/edit")
    Result updateSetmeal(@RequestParam ArrayList<Integer>checkGroupIds ,@RequestBody Setmeal setmeal){
       setmealService.updateSetmeal(checkGroupIds,setmeal);
        return  new Result(true ,"编辑套餐成功");
    }
    //查找套餐-------------------
    @GetMapping("/findById")
    Result findByid(@RequestParam Integer id){
      Setmeal setmeal=  setmealService.findById(id);
return  new Result(true,"查询套餐成功",setmeal );
    }
    //分页查询----------------
    @PostMapping("/findPage")
    PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询套餐：{}", queryPageBean);

        PageResult pageResult=setmealService.findPage(queryPageBean);
        return pageResult;
    }
//删除套餐---------------------
    @GetMapping("/deleteById")
    public Result deleteById(Integer id) {
        log.info("删除套餐：{}", id);
        setmealService.deleteById(id);
        return new Result(true, "删除套餐成功");
    }
    //上传图片------------------
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        log.info("上传图片:{},图片大小:{}kb", imgFile.getName(),imgFile.getSize()/1024);
        String imgUrl = setmealService.upload(imgFile);
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, imgUrl);
    }



}
